﻿using System;
using System.ServiceManager.Facade;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;

namespace ServiceManager.TFSIntegration
{
    public class TFSSettings
    {
        private const string ManagementPackID = "F30960D3-7C16-A0BE-DFB6-6DD8117ACE9D";
        private const string ClassName = "ServiceManager.TFSIntegration.TFSSettings";
        private static readonly Guid TfsAdminSettingsObjectId = new Guid("7D1BDE3E-9D65-5CB1-64F6-53165B1C3F29");

        public string TFSUrl { get; set; }

        public string PortalIncidentUrl { get; set; }

        public Guid Id { get; set; }

        public int SyncFrequency { get; set; }

        public string ResolvedString { get; set; }

        public static TFSSettings Get()
        {
            var tfsSettings = new TFSSettings();
            var settings = SCSMServer.GetObject<EnterpriseManagementObject>(TfsAdminSettingsObjectId);
            var classAdminSetting = GetAdminSettingClass();

            tfsSettings.TFSUrl = settings[classAdminSetting, "TFSUrl"].ToString();
            tfsSettings.PortalIncidentUrl = settings[classAdminSetting, "PortalIncidentURL"].ToString();
            int sync;
            if (int.TryParse(settings[classAdminSetting, "SyncFrequency"].Value.ToString(), out sync))
                tfsSettings.SyncFrequency = sync;
            tfsSettings.ResolvedString = settings[classAdminSetting, "ResolvedString"].ToString();
            tfsSettings.Id = settings.Id;

            return tfsSettings;

        }


        private static ManagementPackClass GetAdminSettingClass()
        {
            var mp = SCSMServer.GetManagementPack(new Guid(ManagementPackID));

            return mp.GetClass(ClassName);
        }

    }
}
